/**
 * 
 *  file add for RGK new UI solution by anxiang.xiao  20160506
 *  this class is used for the mode panel page switch 
 */

package com.android.camera.manager;

import android.view.View;
import android.widget.TextView;
import android.widget.LinearLayout;
import android.view.ViewGroup;

import com.android.camera.CameraActivity;
import com.android.camera.FeatureSwitcher;
import com.android.camera.Log;
import com.android.camera.ModeChecker;
import com.android.camera.R;
import com.android.camera.ui.RgkBottomModeScrollLayout;
import com.android.camera.ui.RgkBottomModeScrollLayout.OnScrollToScreenListener;
import com.mediatek.camera.ISettingCtrl;
import com.mediatek.camera.ICameraAddition.AdditionActionType;
import com.mediatek.camera.ICameraMode.ModeState;
import com.mediatek.camera.platform.ICameraAppUi;
import com.mediatek.camera.platform.ICameraAppUi.ViewState;
import com.mediatek.camera.setting.preference.ListPreference;

import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.widget.Toast;

import com.android.camera.ui.PreviewSurfaceView;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.os.Handler;
import android.os.Message;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation.AnimationListener;

public class RGK_BottomModePicker extends ViewManager implements View.OnClickListener,
        ICameraAppUi.GestureListener {
    private static final String TAG = "RGK_BottomModePicker";

    public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key";
    public static final String KEY_SLOW_MOTION = "pref_slow_motion_key";
    public static final String ENTRY_NIGHT = "night";
    public static final String ENTRY_AUTO = "auto";
    public static final String ENTRY_ON = "on";
    public static final String ENTRY_OFF = "off";
    public static final int MODE_COUNT = 6;
    // public static final int MODE_BEAUTY_INDEX = 0;
    public static final int MODE_PANO_INDEX = 0;
    public static final int MODE_VIDEO_INDEX = 1;
    public static final int MODE_PHOTO_INDEX = 2;
    public static final int MODE_SLOWMOTION_INDEX = 3;
    public static final int MODE_PROFESSION_INDEX = 4;
    public static final int MODE_NIGHT_INDEX = 5;
    private static final int MODE_DEFAULT = MODE_PHOTO_INDEX;

    public static final int[] MODE_IDS = new int[MODE_COUNT];
    public static final int[] MODES = new int[MODE_COUNT];

    private boolean mIsScrolling = false;
    private boolean mIsModeSwitching = false;
    private boolean mIsSwitchToSlowMotionPending;
    private TextView faceTxtView;
    private TextView mModePano;
    private TextView mModePhoto;
    private TextView mModeVideo;
    private TextView mModeSlowmotion;
    private TextView mModeProf;
    private TextView mModeNight;
    private ImageView mSwitchImageView;

    private CameraActivity mContext;

    private ListPreference mEffectPreference;
    protected ISettingCtrl mISettingCtrl;

    private RgkBottomModeScrollLayout mScrollLayout;

    private static final int FLING_MIN_DISTANCE = 100;

    static {
        // MODE_IDS[MODE_BEAUTY_INDEX] = R.id.face_buty;
        MODE_IDS[MODE_PANO_INDEX] = R.id.mode_pano;
        MODE_IDS[MODE_VIDEO_INDEX] = R.id.mode_video;
        MODE_IDS[MODE_PHOTO_INDEX] = R.id.mode_photo;
        MODE_IDS[MODE_SLOWMOTION_INDEX] = R.id.mode_slowmotion;
        MODE_IDS[MODE_PROFESSION_INDEX] = R.id.mode_profession;
        MODE_IDS[MODE_NIGHT_INDEX] = R.id.mode_night;

        // MODES[MODE_BEAUTY_INDEX] = ModePicker.MODE_FACE_BEAUTY;
        MODES[MODE_PANO_INDEX] = ModePicker.MODE_PANORAMA;
        MODES[MODE_VIDEO_INDEX] = ModePicker.MODE_VIDEO;
        MODES[MODE_PHOTO_INDEX] = ModePicker.MODE_PHOTO;
        MODES[MODE_SLOWMOTION_INDEX] = ModePicker.MODE_SLOWMOTION;
        MODES[MODE_PROFESSION_INDEX] = ModePicker.MODE_PHOTO;
        MODES[MODE_NIGHT_INDEX] = ModePicker.MODE_PHOTO; // need to be modify
                                                         // later
    }

    private static final int MSG_MODE_SWITCH_START = 1;
    private static final int MSG_MODE_SWITCH_RUNNING = 2;
    private static final int MSG_MODE_SWITCH_END = 3;
    private int mWhichScreen = MODE_DEFAULT;
    private TranslateAnimation mToLeftAnim;
    private TranslateAnimation mToRightAnim;
    // private ScaleAnimation mScaleAnim;
    private AlphaAnimation alphaIn;
    private AlphaAnimation alphaOut;

    private static final long MAX_DELAY = 500;
    private long delayMillis = 0;

    private AnimationListener alphaOutListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mSwitchImageView.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    };

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_MODE_SWITCH_START:
                    Log.d(TAG, "RGK_BottomModePicker->handleMessage(MSG_MODE_SWITCH_START)");

                    closeNightMode();
                    closeProfessionMode();
                    closeSlowmotionMode();

                    if (mWhichScreen != MODE_PANO_INDEX && mWhichScreen != MODE_VIDEO_INDEX) {
                        mWhichScreen = MODE_PHOTO_INDEX;
                    }

                    // mIsScrolling = false;

                    if (mSwitchImageView == null) {
                        mSwitchImageView = mContext.getSwitchModeCoverView();
                    }
                    /*
                     * PreviewSurfaceView psv =
                     * mContext.getPreviewSurfaceView(); LayoutParams params =
                     * mSwitchImageView.getLayoutParams(); params.width =
                     * psv.getWidth(); params.height = psv.getHeight();
                     * Log.d(TAG, "[handleMessage]params.width="+params.width+
                     * ",params.height="+params.height);
                     * mSwitchImageView.setLayoutParams(params);
                     */

                    mSwitchImageView.setPadding(0, mContext.getPreviewSurfaceViewPaddingTop(), 0,
                            0);
                    mSwitchImageView.setVisibility(View.VISIBLE);
                    // playAnimation(mWhichScreen, msg.arg1);
                    mSwitchImageView.startAnimation(alphaIn);

                    Message m = mHandler.obtainMessage(MSG_MODE_SWITCH_RUNNING, msg.arg1, 0);
                    mHandler.sendMessageDelayed(m, 330);
                    break;

                case MSG_MODE_SWITCH_RUNNING:
                    Log.d(TAG, "RGK_BottomModePicker->handleMessage(MSG_MODE_SWITCH_RUNNING)");
                    // delayMillis = System.currentTimeMillis();
                    switchMode(msg.arg1);
                    /*
                     * delayMillis = System.currentTimeMillis() - delayMillis;
                     * delayMillis = MAX_DELAY - delayMillis; if (delayMillis >
                     * 0) {
                     * mHandler.sendEmptyMessageDelayed(MSG_MODE_SWITCH_END,
                     * delayMillis); } else {
                     */
                    mHandler.sendEmptyMessage(MSG_MODE_SWITCH_END);
                    // }
                    break;

                case MSG_MODE_SWITCH_END:
                    Log.d(TAG, "RGK_BottomModePicker->handleMessage(MSG_MODE_SWITCH_END)");
                    if (!mIsSwitchToSlowMotionPending) {
                        mSwitchImageView.startAnimation(alphaOut);
                        // mSwitchImageView.setVisibility(View.GONE);
                        mScrollLayout.onModeChangeSuccess();

                        // bug-id:DWYQYSB-305 disable shuttermanager during
                        // switching mode
                        ((CameraActivity) mContext).getShutterManager().setEnabled(true);
                        ((CameraActivity) mContext).getPickerManager().refresh();
                    }
                    break;
            }
        }

    };

    public static int findModeId(int index) {
        if (index < 0 || index > MODE_COUNT) {
            return 0;
        }
        return MODE_IDS[index];
    }

    public static int findIndexById(int resId) {
        for (int i = 0; i < MODE_COUNT; i++) {
            if (MODE_IDS[i] == resId) {
                return i;
            }
        }
        return -1;
    }

    public static int findMode(int index) {
        if (index < 0 || index > MODE_COUNT) {
            return 0;
        }
        return MODES[index];
    }

    public static int findIndexByMode(int mode) {
        for (int i = 0; i < MODE_COUNT; i++) {
            if (MODES[i] == mode) {
                return i;
            }
        }
        return -1;
    }

    private OnScrollToScreenListener mOnScrollToScreenListener = new OnScrollToScreenListener() {

        @Override
        public void doAction(int whichScreen) {
            Log.d(TAG, "mOnScrollToScreenListener===doAction:-----------whichScreen="
                    + whichScreen + ", mWhichScreen=" + mWhichScreen);

//            if (whichScreen == mWhichScreen) {
//                mScrollLayout.onModeChangeSuccess();
//                return;
//            }

            if ((mWhichScreen == 0 || mWhichScreen == MODE_COUNT - 1)
                    && mWhichScreen == whichScreen) {
                mScrollLayout.onModeChangeSuccess();
                return;
            }

            if (mWhichScreen == MODE_PANO_INDEX && mWhichScreen != whichScreen) {
                mContext.getCameraAppUI().dismissInfo();
            }

            mContext.onModeScrollStart();
            ((CameraActivity) mContext).getShutterManager().setEnabled(false);

            // add for camera preview cover when mode is switching
            // if (mWhichScreen != -1) {
            mHandler.obtainMessage(MSG_MODE_SWITCH_START, whichScreen, 0).sendToTarget();
            /*
             * } else { mIsModeSwitching = true; switchMode(whichScreen);
             * mIsModeSwitching = false; mScrollLayout.onModeChangeSuccess(); }
             */

            /*
             * if (whichScreen == MODE_BEAUTY_INDEX) { mIsModeSwitching = true;
             * //mIsScrolling = false;
             * mContext.getModePicker().setCurrentMode(ModePicker.
             * MODE_FACE_BEAUTY);
             * mContext.getShutterManager().bringPhotoTofront();
             * mIsModeSwitching = false; mScrollLayout.onModeChangeSuccess(); }
             * else if (whichScreen == MODE_PHOTO_INDEX) { mIsModeSwitching =
             * true; //mIsScrolling = false;
             * mContext.getModePicker().setCurrentMode(ModePicker.MODE_PHOTO);
             * mContext.getShutterManager().bringPhotoTofront();
             * mIsModeSwitching = false; mScrollLayout.onModeChangeSuccess(); }
             * else if (whichScreen == MODE_VIDEO_INDEX) { mIsModeSwitching =
             * true; //mIsScrolling = false;
             * mContext.getModePicker().setCurrentMode(ModePicker.MODE_VIDEO);
             * mContext.getShutterManager().bringVideoTofront();
             * mIsModeSwitching = false; mScrollLayout.onModeChangeSuccess();
             * }else if (whichScreen == MODE_PROFESSION_INDEX) {
             * mIsModeSwitching = true; //mIsScrolling = false;
             * mContext.getModePicker().setCurrentMode(ModePicker.MODE_PHOTO);
             * changeToProfessionMode();
             * mContext.getShutterManager().bringPhotoTofront();
             * mIsModeSwitching = false; mScrollLayout.onModeChangeSuccess();
             * }else if (whichScreen == MODE_NIGHT_INDEX) { mIsModeSwitching =
             * true; //mIsScrolling = false; // need to modify later
             * mContext.getModePicker().setCurrentMode(ModePicker.MODE_PHOTO);
             * changeToNightMode();//mode special action
             * mContext.getShutterManager().bringPhotoTofront();
             * mIsModeSwitching = false; mScrollLayout.onModeChangeSuccess(); }
             */
        }

        @Override
        public void setIsModeSwitching(boolean switching) {
            mIsModeSwitching = switching;
        }
    };

    public RGK_BottomModePicker(CameraActivity context) {
        super(context, VIEW_LAYER_MODULES);
        setFileter(false);
        mContext = context;

        createAnimation();
    }

    // add animation for mode switch
    private void createAnimation() {
        /*
         * mToLeftAnim = new TranslateAnimation( Animation.RELATIVE_TO_SELF,
         * 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
         * 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
         * mToLeftAnim.setInterpolator(new DecelerateInterpolator());
         * mToLeftAnim.setDuration(300); mToRightAnim = new TranslateAnimation(
         * Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
         * Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
         * mToRightAnim.setInterpolator(new DecelerateInterpolator());
         * mToRightAnim.setDuration(300);
         */

        /*
         * mScaleAnim = new ScaleAnimation(0, 1, 0.0f, 1,
         * Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT,
         * 0.5f); mScaleAnim.setInterpolator(new DecelerateInterpolator());
         * mScaleAnim.setDuration(300);
         */

        alphaIn = new AlphaAnimation(0.0f, 0.96f);
        alphaIn.setDuration(300);
        alphaOut = new AlphaAnimation(0.96f, 0.0f);
        alphaOut.setDuration(300);
        alphaOut.setAnimationListener(alphaOutListener);
    }

    private void playAnimation(int oldMode, int newMode) {
        if (newMode > oldMode) {
            mSwitchImageView.startAnimation(/* mScaleAnim */mToLeftAnim);
        } else {
            mSwitchImageView.startAnimation(/* mScaleAnim */mToRightAnim);
        }
    }

    private void switchMode(int whichScreen) {
        Log.d(TAG, "RGK_BottomModePicker->[switchMode] whichScreen=" + whichScreen);
        if (mContext.getModePicker() == null || mContext.getShutterManager() == null) {
            return;
        }
        mWhichScreen = whichScreen;

        switch (whichScreen) {
            /*
             * case MODE_BEAUTY_INDEX:
             * mContext.getModePicker().setCurrentMode(ModePicker.
             * MODE_FACE_BEAUTY);
             * mContext.getShutterManager().bringPhotoTofront(); break;
             */
            case MODE_PANO_INDEX:
                mContext.getModePicker().setCurrentMode(ModePicker.MODE_PANORAMA);
                mContext.getShutterManager().bringPhotoTofront();
                break;
            case MODE_VIDEO_INDEX:
                mContext.getModePicker().setCurrentMode(ModePicker.MODE_PHOTO);
                mContext.getShutterManager().bringVideoTofront();
                break;
            case MODE_PHOTO_INDEX:
                mContext.getModePicker().setCurrentMode(ModePicker.MODE_PHOTO);
                mContext.getShutterManager().bringPhotoTofront();
                break;
            case MODE_SLOWMOTION_INDEX:
                mContext.getModePicker().setCurrentMode(ModePicker.MODE_PHOTO);
                openSlowmotionMode();
                mContext.getShutterManager().bringVideoTofront();
                break;
            case MODE_PROFESSION_INDEX:
                mContext.getModePicker().setCurrentMode(ModePicker.MODE_PHOTO);
                changeToProfessionMode();
                mContext.getShutterManager().bringPhotoTofront();
                break;
            case MODE_NIGHT_INDEX:
                // need to modify later
                mContext.getModePicker().setCurrentMode(ModePicker.MODE_PHOTO);
                changeToNightMode();// mode special action
                mContext.getShutterManager().bringPhotoTofront();
                break;
        }
    }

    public int getModeIndex() {
        return mWhichScreen;
    }

    private void changeToNightMode() {
        mISettingCtrl = mContext.getISettingCtrl();
        mEffectPreference = mISettingCtrl.getListPreference(KEY_SCENE_MODE);

        if (!mEffectPreference.getValue().equals(ENTRY_NIGHT)) {
            mContext.onSettingChanged(KEY_SCENE_MODE, ENTRY_NIGHT);
            // mEffectPreference.setValue(mEffectPreference.getEntryValues()[i].toString());
            mEffectPreference.setValue(ENTRY_NIGHT);
            if (mContext.getCameraDeviceCtrl() != null) {
                mContext.getCameraDeviceCtrl().applyParameters(false);
            }
            mContext.notifyPreferenceChanged(mEffectPreference);
        }
    }

    private void closeNightMode() {
        mISettingCtrl = mContext.getISettingCtrl();
        mEffectPreference = mISettingCtrl.getListPreference(KEY_SCENE_MODE);
        // M bug-id:DWYQYSB-345 back to normal mode wrong
        if (mEffectPreference != null && mEffectPreference.getValue().equals(ENTRY_NIGHT)) {
            mContext.onSettingChanged(KEY_SCENE_MODE, ENTRY_AUTO);
            mEffectPreference.setValue(ENTRY_AUTO);
            if (mContext.getCameraDeviceCtrl() != null) {
                mContext.getCameraDeviceCtrl().applyParameters(false);
            }
            mContext.notifyPreferenceChanged(mEffectPreference);
        }
    }

    private void changeToProfessionMode() {
        if (!mContext.getRGKProManager().isShowing()) {
            mContext.getRGKProManager().show();
        }
    }

    private void closeProfessionMode() {
        if (mContext.getRGKProManager().isShowing()) {
            mContext.getRGKProManager().hide();
        }
    }

    private void openSlowmotionMode() {
        mISettingCtrl = mContext.getISettingCtrl();
        mEffectPreference = mISettingCtrl.getListPreference(KEY_SLOW_MOTION);
        if (mEffectPreference == null && mContext.isFrontCamera()) {
            mContext.switchCamera(mContext.getDeviceManager().getBackCameraId());
            mIsSwitchToSlowMotionPending = true;
            return;
        }

        if (mEffectPreference != null && !mEffectPreference.getValue().equals(ENTRY_ON)) {
            mContext.onSettingChanged(KEY_SLOW_MOTION, ENTRY_ON);
            // mEffectPreference.setValue(mEffectPreference.getEntryValues()[i].toString());
            mEffectPreference.setValue(ENTRY_ON);
            if (mContext.getCameraDeviceCtrl() != null) {
                mContext.getCameraDeviceCtrl().applyParameters(false);
            }
            mContext.notifyPreferenceChanged(mEffectPreference);
        }
    }

    private void closeSlowmotionMode() {
        mISettingCtrl = mContext.getISettingCtrl();
        mEffectPreference = mISettingCtrl.getListPreference(KEY_SLOW_MOTION);

        if (mEffectPreference != null && mEffectPreference.getValue().equals(ENTRY_ON)) {
            mContext.onSettingChanged(KEY_SLOW_MOTION, ENTRY_OFF);
            // mEffectPreference.setValue(mEffectPreference.getEntryValues()[i].toString());
            mEffectPreference.setValue(ENTRY_OFF);
            if (mContext.getCameraDeviceCtrl() != null) {
                mContext.getCameraDeviceCtrl().applyParameters(false);
            }
            mContext.notifyPreferenceChanged(mEffectPreference);
        }
    }

    public void scrollToDefault() {
        if (!mContext.canModeBeSwitched()) {
            return;
        }
        if (mIsModeSwitching) {
            Log.d(TAG, "RGK_BottomModePicker [onClick]===mIsModeSwitching == true, return.");
            return;
        }
        if (mScrollLayout.getCurScreen() != MODE_DEFAULT) {
            mScrollLayout.setToScreen(MODE_DEFAULT);
        }
    }

    @Override
    public void onClick(View view) {
        // A bug-id:DWYQYSB-161 not support click when recording
        // Add nonepicker for jude call from messsage or not ,disalbe scroll for
        // message app
        if (!mContext.canModeBeSwitched()) {
            return;
        }

        Log.e(TAG, "onClick current mode:" + mContext.getModePicker().getCurrentMode());

        /*
         * if (mIsScrolling) { Log.d(TAG,
         * "RGK_BottomModePicker [onClick]===mIsScrolling == true, return.");
         * return; }
         */

        if (mIsModeSwitching) {
            Log.d(TAG, "RGK_BottomModePicker [onClick]===mIsModeSwitching == true, return.");
            return;
        }

        /*
         * if (faceTxtView == view && mScrollLayout.getCurScreen() !=
         * MODE_BEAUTY_INDEX) { mIsScrolling = true;
         * mScrollLayout.setToScreen(MODE_BEAUTY_INDEX); } else
         */
        int toMode = -1;
        mIsScrolling = true;
        if (mModePano == view && mScrollLayout.getCurScreen() != MODE_PANO_INDEX) {
            toMode = MODE_PANO_INDEX;
        } else if (mModeVideo == view && mScrollLayout.getCurScreen() != MODE_VIDEO_INDEX) {
            toMode = MODE_VIDEO_INDEX;
        } else if (mModePhoto == view && mScrollLayout.getCurScreen() != MODE_PHOTO_INDEX) {
            toMode = MODE_PHOTO_INDEX;
        } else if (mModeSlowmotion == view && mScrollLayout.getCurScreen() != MODE_SLOWMOTION_INDEX) {
            toMode = MODE_SLOWMOTION_INDEX;
        } else if (mModeProf == view
                && mScrollLayout.getCurScreen() != MODE_PROFESSION_INDEX) {
            toMode = MODE_PROFESSION_INDEX;
        } else if (mModeNight == view && mScrollLayout.getCurScreen() != MODE_NIGHT_INDEX) {
            toMode = MODE_NIGHT_INDEX;
        } else {
            mIsScrolling = false;
        }
        if (toMode != -1) {
            mScrollLayout.setToScreen(toMode);
        }
    }

    public void setNormalScreenDefault() {

        Log.i(TAG, "xax_[setNormalScreenDefault]");

        if (mContext.getRGKProManager().isShowCameraProContainer())
            return;
        // mScrollLayout.setToScreenForceLy(MODE_PHOTO_INDEX);
        if (mScrollLayout.getCurScreen() == MODE_PHOTO_INDEX) {
            switchMode(MODE_PHOTO_INDEX);
        } else {
            mScrollLayout.setToScreen(MODE_PHOTO_INDEX);
        }

    }

    // bug-id:DWYQYSB-378 scroll set default to video after finish view draw
    // start
    public void setDefaultScreen(int screen) {
        mScrollLayout.setDefaultScreen(screen);
    }

    public void setVideoScreenDefault() {

        if (mContext.getRGKProManager().isShowCameraProContainer() || mScrollLayout == null)
            return;
        // mScrollLayout.setToScreenForceLy(MODE_PHOTO_INDEX);

        // mScrollLayout.setToScreenForceLy(MODE_PHOTO_INDEX);
        if (mScrollLayout.getCurScreen() == MODE_VIDEO_INDEX) {
            switchMode(MODE_VIDEO_INDEX);
        } else {
            mScrollLayout.setToScreen(MODE_VIDEO_INDEX);
        }

    }
    // bug-id:DWYQYSB-378 scroll set default to video after finish view draw end

    public boolean isSwitchToSlowMotionPending() {
        return mIsSwitchToSlowMotionPending;
    }

    public void onCameraOpenDone() {
        if (mIsSwitchToSlowMotionPending) {
            openSlowmotionMode();
            mIsSwitchToSlowMotionPending = false;
            mHandler.sendEmptyMessage(MSG_MODE_SWITCH_END);
        }
    }

    public boolean isModeSwitching() {
        return mIsModeSwitching;
    }

    public void release() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        if (alphaIn != null) {
            alphaIn.cancel();
        }
        if (alphaOut != null) {
            alphaOut.cancel();
        }
        if (mSwitchImageView != null) {
            mSwitchImageView.setVisibility(View.GONE);
        }
        mIsScrolling = false;
        mIsModeSwitching = false;
        mIsSwitchToSlowMotionPending = false;
        if (mScrollLayout != null) {
            mScrollLayout.release();
        }
    }

    public void restore() {
        if (mScrollLayout != null) {
            mScrollLayout.restore(mWhichScreen);
        }
    }

    @Override
    protected View getView() {
        View view = inflate(R.layout.rgk_bottom_scroll_mode);
        // faceTxtView = (TextView) view.findViewById(R.id.face_buty);
        // faceTxtView.setText(mContext.getResources().getString(R.string.pref_camera_activity_beauty));
        // faceTxtView.setOnClickListener(this);
        // faceTxtView.setTextColor(mContext.getResources().getColor(R.color.mode_not_chose_color));

        mModePano = (TextView) view.findViewById(R.id.mode_pano);
        mModePano.setOnClickListener(this);

        mModePhoto = (TextView) view.findViewById(R.id.mode_photo);
        mModePhoto.setOnClickListener(this);

        mModeVideo = (TextView) view.findViewById(R.id.mode_video);
        mModeVideo.setOnClickListener(this);

        mModeSlowmotion = (TextView) view.findViewById(R.id.mode_slowmotion);
        mModeSlowmotion.setOnClickListener(this);

        mModeProf = (TextView) view.findViewById(R.id.mode_profession);
        mModeProf.setOnClickListener(this);

        mModeNight = (TextView) view.findViewById(R.id.mode_night);
        mModeNight.setOnClickListener(this);

        mScrollLayout = (RgkBottomModeScrollLayout) view
                .findViewById(R.id.mode_picker_scroll_layout);
        mScrollLayout.setOnScrollToScreen(mOnScrollToScreenListener);

        mContext.setGestureListener(this);

        return view;
    }

    public void onRefresh() {
        Log.i(TAG, "onRefresh");
        // mContext.setGestureListener(this);

    }

    public void setBottomPickerGestureListener() {
        mContext.setGestureListener(this);
    }

    /*****************************
     * Gesture Listener
     **************************************/
    @Override
    public boolean onDown(float x, float y, int width, int height) {
        Log.e(TAG, "onDown");

        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.e(TAG, "onFling");

        float totalX = e2.getX() - e1.getX();
        float totalY = e2.getY() - e1.getY();

        // A bug-id:DWYQYSB-161 for not support click when recording
        // Add nonepicker for jude call from messsage or not ,disalbe scroll for
        // message app
        if (!mContext.canModeBeSwitched()) {
            return false;
        }

        if (Math.abs(totalX) < Math.abs(totalY)) {
            return false;
        }

        Log.e(TAG, "onScroll");
        if (mIsScrolling) {
            Log.d(TAG, "RGK_BottomModePicker [onScroll]===mIsScrolling == true, return.");
            return true;
        }

        if (!isShowing())
            return false;

        /*
         * if (mContext.getRGKProManager().isShowCameraProContainer()) return
         * true;
         */

        if (mIsModeSwitching) {
            Log.d(TAG, "RGK_BottomModePicker [onScroll]===mIsModeSwitching == true, return.");
            return true;
        }

        if (totalX <= -FLING_MIN_DISTANCE) {
            if (mScrollLayout.getCurScreen() < MODE_COUNT - 1) {
                mIsScrolling = true;
                mScrollLayout.setToScreen(mScrollLayout.getCurScreen() + 1);
            }
            return true;
        } else if (totalX >= FLING_MIN_DISTANCE) {
            if (mScrollLayout.getCurScreen() > 0) {
                mIsScrolling = true;
                mScrollLayout.setToScreen(mScrollLayout.getCurScreen() - 1);
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean onScroll(float dx, float dy, float totalX, float totalY) {
        return false;
    }

    @Override
    public boolean onSingleTapUp(float x, float y) {
        Log.e(TAG, "onSingleTapUp");
        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(float x, float y) {
        return false;
    }

    @Override
    public boolean onUp() {
        Log.e(TAG, "onUp");

        mIsScrolling = false;

        return false;
    }

    @Override
    public boolean onDoubleTap(float x, float y) {
        Log.e(TAG, "onDoubleTap");
        return false;
    }

    @Override
    public boolean onScale(float focusX, float focusY, float scale) {
        return false;
    }

    @Override
    public boolean onScaleBegin(float focusX, float focusY) {
        return false;
    }

    @Override
    public boolean onLongPress(float x, float y) {
        return false;
    }

}
